<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/14
 * Time: 04:07
 */

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\Curl;
use App\Providers\MyJwt;
use App\Services\ApartService;
use App\Services\ContractService;
use App\Services\Service;
use App\Services\UserService;
use Illuminate\Http\Request;

class DefaultController extends Controller
{

    public function regions(Request $request)
    {
        $this->sendGet(function() use($request) {
            $data = Service::getRegions();

            $this->sendJson($data);
        });
    }

    public function standards(Request $request)
    {
        $this->sendGet(function() use($request) {
            $data = Service::getStandards();

            $this->sendJson($data);
        });
    }

    public function programs(Request $request)
    {
        $this->sendGet(function() use($request) {
            $data = Service::getPrograms();

            $this->sendJson($data);
        });
    }

    public function programUsers(Request $request)
    {
        $this->sendGet(function() use($request) {
            $programId = $request->get('program_id');
            $data = Service::getProgramUsers($programId);

            $this->sendJson($data);
        });
    }

    public function programAparts(Request $request)
    {
        $this->sendGet(function() use($request) {
            $programId = $request->get('program_id');
            $data = Service::getProgramAparts($programId);

            $this->sendJson($data);
        });
    }

    public function aparts(Request $request)
    {
        $this->sendGet(function() use($request) {
            $data = ApartService::getAparts();

            $this->sendJson($data);
        });
    }

    public function contractTemplate(Request $request)
    {
        $this->sendGet(function() use($request) {
            $contractId = $request->get('contract_id');
            $data = ContractService::getContractTemplate($contractId);

            $this->sendJson($data);
        });
    }

    public function info(Request $request)
    {
        $this->sendGet(function() use($request){
            $authorization = $request->header('Authorization');
            $jwt = new MyJwt();
            $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
            $data = UserService::info($jwtCode['uid']);
            $this->sendJson($data);
        });
    }

    public function getToken(Request $request)
    {
        $this->sendGet(function() use($request){
            header('Content-Type: text/html; charset=utf-8');
            $appId = 'MSG85148937';
            $appPassword = 'ZWM2YzEwMTJlOGQ1ZDIyMDBhMTkxMTQ1NDA2YWIxNDk';
            $url = 'https://uc.hznu.edu.cn:8080/msg/getThirdAPIToken';
            $data = Curl::get($url, ['appId' => $appId, 'appPassword' => $appPassword]);
            return $this->sendJson($data, 200, 'appId:'.$appId.', appPassword:'.$appPassword);
        });
    }

    public function sendSms(Request $request)
    {
        $this->sendPost(function() use($request) {
            header('Content-Type: text/html; charset=utf-8');

            $url = 'https://uc.hznu.edu.cn:8080/msg/sendThirdMessage';
            $data = [
                "token" => '65035e94-5508-4021-b5ff-e1eb5cba9e85',
                "msgtitle" => 'hello friend',
                "msgContent" => 'it is very glad to hear you',
                "msgType=" => 1,
                "sendStatus" => 1,
                "sendChannel" => 'SMS',
                "sendMode" => 'normal',
                "receivers" => '[{"type": "User","id": "20040056","name": "陆宏维"}]',
                "summary" => '测试消息',
                "isReply" => 1
                ];

            $data = Curl::post($url, $data);
            return $this->sendJson($data);
        });
    }
}